

namespace Stee.CAP8.MessageBus.Interface.Models
{
    using System;
    using Stee.CAP8.MessageBus.Interface.Enums;

    /// <summary>
    /// Published message outcome containing published message, state and timestamp
    /// </summary>
    public class PublishOutcome
    {
        /// <summary>
        /// Gets the published message
        /// </summary>
        public byte[] Message { get; }

        /// <summary>
        /// Gets the published message's persist state
        /// </summary>
        public PersistenceState PersistState { get; }

        /// <summary>
        /// Gets the published timestamp
        /// </summary>
        public DateTime Timestamp { get; }

        /// <summary>
        /// Gets the error flag
        /// </summary>
        public bool HasError { get; }

        /// <summary>
        /// Gets the error message
        /// </summary>
        public string Error { get; }


        /// <summary>
        /// Initializes a new instance of the PublishOutcome class
        /// </summary>
        /// <param name="message">Published message</param>
        /// <param name="persistState">Persisted state</param>
        /// <param name="timestamp">Published timestamp</param>
        /// <param name="hasError">Error flag</param>
        /// <param name="error">Error message</param>
        public PublishOutcome(byte[] message, PersistenceState persistState, DateTime timestamp, bool hasError = false, string error = null)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            else if (timestamp == null)
            {
                throw new ArgumentNullException(nameof(timestamp));
            }
            else if (hasError && string.IsNullOrEmpty(error))
            {
                throw new ArgumentNullException(nameof(error));
            }

            this.Message = message;
            this.PersistState = persistState;
            this.Timestamp = timestamp;
            this.HasError = hasError;
            this.Error = error;
        }
    }
}
